package org.oep.jmingle;
import org.oep.widgets.MonologueWidget;

import java.io.IOException;
import java.util.Hashtable;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.BluetoothStateException;


public class TutorialScreen extends GameCanvas implements Runnable {
	private Actor minglet;
	private Thread gameThread;
	private MonologueWidget monoBox;
	private EventPipe eventHandler;
	
	private TiledLayer background;
	private int Y_MIN;
	
	public TutorialScreen() {
		super(false);
		construct();
		toInitialState();
	}
	
	public void keyPressed(int keyCode) {
		keyCode = this.getGameAction(keyCode);
		
		if(keyCode == GameCanvas.FIRE) {
			if(monoBox.isActive())
				monoBox.action();
			else if(eventHandler != null) {
				eventHandler.eventReceived(JMingle.SHOW_MAIN);
			}
		}
	}

	public void run() {
		while(true) {
			minglet.nextFrame();
			monoBox.tick();
			
			repaint();
			try {
				Thread.sleep(83);
			} catch(Exception e) {
				
			}
		}
	}
	
	public void paint(Graphics g) {
		g.setColor(0xFFFFFF);
		g.fillRect(0,0,getHeight(),getWidth());
		
		background.paint(g);
		minglet.paint(g);
		
		if(monoBox.isActive())
			monoBox.paint(g);
		else
			monoBox.flushGraphics(this);
	}

	public void construct() {
		String hash, name;
		try {
			hash = LocalDevice.getLocalDevice().getBluetoothAddress();
			name = LocalDevice.getLocalDevice().getFriendlyName();
		}
		catch(BluetoothStateException e) {
			name = "BT off";
			hash = "RAR";
		}
		
		buildBackground();
		minglet = Utils.makeActor(hash, name);
		monoBox = new MonologueWidget(getWidth()/16, 10, (14 * getWidth()) / 16, 3, 3);
		monoBox.setFont(JMingle.DEFAULT_FONT);
	}
	
	private void buildBackground( ) {
		int width = getWidth();
		int height = getHeight();
		try {
			Image im = Image.createImage("/res/graphics/bg-tiles.png");
			
			int	xTiles = 1 + (int) Math.ceil( width / 16 );
			int	yTiles = 1 + (int) Math.ceil( height / 16 );
			background = new TiledLayer(xTiles, yTiles, im, 16, 16);
			
			Y_MIN = (16 * yTiles) - (4 * 16);
			
			for(int i = 0; i < yTiles; i++) {
				for(int j = 0; j < xTiles; j++) {
					// Grass
					if(i > yTiles - 4)
						background.setCell(j, i, 7);
					// Grass tips
					else if(i == yTiles - 4)
						background.setCell(j, i, 6);
					// Sky
					else
						background.setCell(j, i, 5);
				}
			}
			
			// Sun
			background.setCell(xTiles - 3, 1, 1);
			background.setCell(xTiles - 2, 1, 2);
			background.setCell(xTiles - 3, 2, 3);
			background.setCell(xTiles - 2, 2, 4);
		
		}
		catch(IOException e) {
			
		}
	}
	
	public void setEventPipe(EventPipe e) {
		eventHandler = e;
	}
	
	public void toInitialState() {
		minglet.setPosition(getWidth() / 2, (Y_MIN + getHeight()) / 2);
		
		Hashtable t = new Hashtable();
		t.put("name", minglet.getName());
		monoBox.setSubTable(t);
		monoBox.clearBuffer();
		monoBox.loadFile("/res/text/intro.txt");
		monoBox.setActive(true);
		monoBox.setSubTable(t);
	}
	
	public void pause() {
		if(gameThread != null && gameThread.isAlive()) {
			gameThread.interrupt();
		}
	}
	
	public void start() {
		if(gameThread == null || !gameThread.isAlive()) {
			gameThread = new Thread(this);
			gameThread.start();
		}
	}
}
